package com.thaiopensource.relaxng.pattern;

import java.util.ArrayList;
import java.util.List;

class DuplicateAttributeDetector
{
  private final List <NameClass> nameClasses = new ArrayList <NameClass> ();
  private Alternative alternatives = null;

  private static class Alternative
  {
    private final int startIndex;
    private int endIndex;
    private final Alternative parent;

    private Alternative (final int startIndex, final Alternative parent)
    {
      this.startIndex = startIndex;
      this.endIndex = startIndex;
      this.parent = parent;
    }
  }

  void addAttribute (final NameClass nc) throws RestrictionViolationException
  {
    int lim = nameClasses.size ();
    for (Alternative a = alternatives; a != null; a = a.parent)
    {
      for (int i = a.endIndex; i < lim; i++)
        checkAttributeOverlap (nc, nameClasses.get (i));
      lim = a.startIndex;
    }
    for (int i = 0; i < lim; i++)
      checkAttributeOverlap (nc, nameClasses.get (i));
    nameClasses.add (nc);
  }

  static private void checkAttributeOverlap (final NameClass nc1, final NameClass nc2) throws RestrictionViolationException
  {
    OverlapDetector.checkOverlap (nc1, nc2, "duplicate_attribute_name", "duplicate_attribute_ns", "duplicate_attribute");
  }

  void startChoice ()
  {
    alternatives = new Alternative (nameClasses.size (), alternatives);
  }

  void alternative ()
  {
    alternatives.endIndex = nameClasses.size ();
  }

  void endChoice ()
  {
    alternatives = alternatives.parent;
  }

}
